import { shop_fav, shop_favlist, shop_unfav } from "@/utils/api";

export default{
    namespaced:true,
    state:{
        collectlist:[]
    },
    mutations:{
        collect(state,payload){
            state.collectlist.push(payload);
        },

        cancel_collect(state,payload){
            var index=state.collectlist.findIndex((item)=>{
                return item.mtWmPoiId==payload.mtWmPoiId;
            });
            if(index!=-1){
                state.collectlist.splice(index,1);
            }
        },

        save_favlist(state,payload){
            console.log(payload);
            state.collectlist=payload;
        }
    },
    actions:{
        request_favlist(store){
            var userid=JSON.parse(localStorage.getItem('userinfo')).id; 
            shop_favlist({userid}).then((res)=>{
                store.commit('save_favlist',res.data.list)
            })
        },
        request_fav(store,payload){
            var userid=JSON.parse(localStorage.getItem('userinfo')).id;
            shop_fav({userid,shopid:payload.mtWmPoiId}).then((res)=>{
                if(res.data.code==200){
                    store.commit('collect',payload);
                }
            })
        },
        request_unfav(store,payload){
            var userid=JSON.parse(localStorage.getItem('userinfo')).id;
            shop_unfav({userid,shopid:payload.mtWmPoiId}).then((res)=>{
                if(res.data.code==200){
                    store.commit('cancel_collect',payload);
                }
            })
        }
    }
}